package com.myproject.thread.interrupt;

//没有阻塞的情况，调用interrupt会将isInterrupted状态改为true
public class IsInterruptTest2 {

    public static boolean flag = true;

    /*
    * 此种写法，主线程调用interrupt方法后，再打印线程的isInterrupted会有两种结果 true或者false(随机出现)
    * 这是因为调用interrupt方法会将中断标志位改为true，线程阻塞时如果发现自己的中断标志位为true，则退出阻塞状态，并将中断标志位改为false
    * 所以会随机出现
    * */
    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            System.out.println("start");
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getName()+ " " +Thread.currentThread().isInterrupted());
            }

            while (flag){}
            System.out.println("end");
        });
        t1.start();
        Thread.sleep(1000);

        System.out.println("t1 isInterrupted state is " + t1.isInterrupted());
        t1.interrupt();
        System.out.println("t1 isInterrupted state is " + t1.isInterrupted());

    }

}
